package ltd.dujiabao.zookeeper.watcher;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author : DuJiabao
 * @Project : zookeeper
 * @Time : 2021/5/3 11:37
 * @Desc :
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class DBConfig {
  private String ip;
  private String username;
  private String password;
  private static final Logger log = Logger.getLogger(DBConfig.class);

  public DBConfig(Map<String, String> config) throws NoSuchFieldException, IllegalAccessException {
    importConfig(config);
    log.info("创建DBConnection成功: " + this.toString());
  }

  public void importConfig(Map<String, String> config) throws NoSuchFieldException, IllegalAccessException {
    for (Map.Entry<String, String> entry : config.entrySet()) {
      String key = entry.getKey();
      key = key.substring(key.lastIndexOf("/") + 1);
      String value = entry.getValue();
      Field field = this.getClass().getDeclaredField(key);
      field.setAccessible(true);
      field.set(this, value);
    }
  }
}
